package com.example.personalizededucationsystem.controller;

import com.example.personalizededucationsystem.dto.dto.UserDetailResponseDto;
import com.example.personalizededucationsystem.dto.request.FileRequest;
import com.example.personalizededucationsystem.dto.request.UpdateNickNameRequest;
import com.example.personalizededucationsystem.dto.request.UpdateUserPasswordRequest;
import com.example.personalizededucationsystem.dto.response.UpdateProfileResponse;
import com.example.personalizededucationsystem.dto.response.UploadFileResponse;
import com.example.personalizededucationsystem.model.User;
import com.example.personalizededucationsystem.service.UserService;
import com.example.personalizededucationsystem.utils.IP_VALUE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户个人信息修改
 */

@RestController
@CrossOrigin(origins = IP_VALUE.IP)
@PreAuthorize("isAuthenticated()")
@RequestMapping("/api/user")
public class UserProfileController {
    private final UserService userService;

    @Autowired
    public UserProfileController(UserService userService) {
        this.userService = userService;
    }

    @PutMapping("/name/request")
    public ResponseEntity<UpdateProfileResponse> updateName(@RequestBody UpdateNickNameRequest request) {
        return ResponseEntity.ok(userService.updateNickName(request));
    }

    @PutMapping("/password/request")
    public ResponseEntity<UpdateProfileResponse> updatePassword(@RequestBody UpdateUserPasswordRequest request) {
        return ResponseEntity.ok(userService.updateUserPassword(request));
    }

    @GetMapping("/profile")
    public ResponseEntity<UserDetailResponseDto> getCurrentUserProfile() {
        return ResponseEntity.ok(userService.getUserProfile());
    }

    @PostMapping("/image")
    public ResponseEntity<UploadFileResponse> uploadFiles(@RequestParam("file") MultipartFile file) {
        return ResponseEntity.ok(userService.handleFilesUpload(file));
    }

    @PutMapping("/update-avatar")
    public ResponseEntity<UpdateProfileResponse> updateAvatar(@RequestBody FileRequest request, @AuthenticationPrincipal User user) {
        return ResponseEntity.ok(userService.updateAvatar(request, user));
    }

    @PutMapping("/update-background")
    public ResponseEntity<UpdateProfileResponse> updateBackground(@RequestBody FileRequest request, @AuthenticationPrincipal User user) {
        return ResponseEntity.ok(userService.updateBackground(request, user));
    }
}
